package com.zl.freight.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.RegexUtils;
import com.zl.freight.R;
import com.zl.freight.base.BaseActivity;
import com.zl.freight.mode.CarInformationEntity;
import com.zl.freight.mode.TopNewsBean;
import com.zl.freight.utils.API;
import com.zl.freight.utils.ImageLoader;
import com.zl.freight.utils.SoapCallback;
import com.zl.freight.utils.SoapUtils;
import com.zl.freight.utils.SpUtils;
import com.zl.zlibrary.dialog.PhotoDialog;
import com.zl.zlibrary.utils.GsonUtils;
import com.zl.zlibrary.utils.ImageFactory;
import com.zl.zlibrary.utils.MiPictureHelper;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdateNewsActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_title_news)
    EditText tvTitleNews;
    @BindView(R.id.iv_add_img)
    ImageView ivAddImg;
    @BindView(R.id.tv_news_content)
    EditText tvNewsContent;
    @BindView(R.id.tv_web_url)
    EditText tvWebUrl;
    private CarInformationEntity entity;
    private PhotoDialog photoDialog;
    private String imagePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_news);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        TopNewsBean data = (TopNewsBean) getIntent().getSerializableExtra("data");
        entity = new CarInformationEntity();
        entity.setId(Integer.parseInt(data.getId()));
        //判断是网页还是文字内容
        if (!TextUtils.isEmpty(data.getInfoContent())) {
            entity.setInfoType(0);
            entity.setInfoContent(data.getInfoContent());
            tvWebUrl.setVisibility(View.GONE);
            tvNewsContent.setText(data.getInfoContent());
        } else {
            entity.setInfoType(1);
            entity.setInfoLink(data.getInfoLink());
            tvNewsContent.setVisibility(View.GONE);
            tvWebUrl.setText(data.getInfoLink());
        }
        entity.setIsPay(data.getIsPay());
        entity.setInfoTitle(data.getInfoTitle());
        entity.setUserId(SpUtils.getUserData(mActivity).getId());
        entity.setInfoKey(SpUtils.getUserData(mActivity).getId());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        entity.setUpdateAt(dateFormat.format(new Date(System.currentTimeMillis())));
        entity.setInfoPic("");

        tvTitleNews.setText(data.getInfoTitle());
        ImageLoader.loadImageUrl(mActivity, data.getInfoPic(), ivAddImg);
    }

    private void initView() {
        tvTitle.setText("修改头条");
        tvTitleRight.setText("提交修改");
        photoDialog = new PhotoDialog(mActivity);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == mActivity.RESULT_OK) {
            switch (requestCode) {
                case PhotoDialog.PICK_FROM_CAMERA:
                    imagePath = photoDialog.imagePath;
                    break;
                case PhotoDialog.SELECT_PHOTO:
                    imagePath = MiPictureHelper.getPath(mActivity, data.getData());
                    break;
            }
            ImageLoader.loadImageFile(imagePath, ivAddImg);
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_title_right, R.id.iv_add_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_title_right:
                commit();
                break;
            case R.id.iv_add_img:
                photoDialog.show(view);
                break;
        }
    }

    /**
     * 提交保存
     */
    private void commit() {
        String title = tvTitleNews.getText().toString().trim();
        String url = tvWebUrl.getText().toString().trim();
        String content = tvNewsContent.getText().toString().trim();
        if (TextUtils.isEmpty(title)) {
            showToast("标题不能为空");
            return;
        }

        if (entity.getInfoType() == 0) {
            if (TextUtils.isEmpty(content)) {
                showToast("文章内容不能为空");
                return;
            }
            entity.setInfoContent(content);
        } else {
            if (TextUtils.isEmpty(url)) {
                showToast("网址不能为空");
                return;
            }

            if (!RegexUtils.isURL(url)) {
                showToast("请输入正确的网址");
                return;
            }

            entity.setInfoLink(url);
        }

        entity.setInfoTitle(title);

        if (!TextUtils.isEmpty(imagePath)) {
            showDialog();
            entity.setInfoPic(ImageFactory.base64Encode(ImageFactory.getimage(imagePath)));
        }

        if (TextUtils.isEmpty(imagePath)) {
            showDialog();
        }

        Map<String, String> params = new HashMap<>();
        params.put("InfoJson", GsonUtils.toJson(entity));
        SoapUtils.Post(mActivity, API.UpdateInfo, params, new SoapCallback() {
            @Override
            public void onError(String error) {
                hideDialog();
                showToast(error);
            }

            @Override
            public void onSuccess(String data) {
                hideDialog();
                showToast("修改成功");
                finish();
            }
        });

    }
}
